package com.gec.hrm.servlet;

import com.gec.hrm.dao.impl.DeptDao;
import com.gec.hrm.dao.impl.DeptDaoImpl;
import com.gec.hrm.dao.impl.JobDao;
import com.gec.hrm.dao.impl.JobDaoImpl;
import com.gec.hrm.pojo.Dept;
import com.gec.hrm.pojo.Job;
import com.gec.hrm.pojo.R;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/jobList.action","/addJob.action","/checkJobName.action","/updJob.action","/delJobs.action","/delJob.action","/getAllJob.action"})
public class JobServlet extends HttpServlet {
    public JobDao jobDao = new JobDaoImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String url = request.getRequestURI();
        System.out.println(url);
        String action = url.substring(url.lastIndexOf("/") + 1);
        System.out.println(action);

        if (action.equals("jobList.action")) {
            jobList(request, response);
        } else if (action.equals("checkJobName.action")) {
            cheackJobName(request, response);
        } else if (action.equals("addJob.action")) {
            addJob(request, response);
        } else if (action.equals("updJob.action")) {
            updJob(request, response);
        } else if (action.equals("delJobs.action")) {
            delJobs(request, response);
        } else if (action.equals("delJob.action")) {
            delJob(request, response);
        }else if (action.equals("getAllJob.action")) {
            getAllJob(request, response);
        }
    }

    private void getAllJob(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding(("UTF-8"));
        PrintWriter out =response.getWriter();
        List<Job> allJob = jobDao.getAllJob();
        out.print(new Gson().toJson(allJob));
    }

    private void delJob(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));

        try {
            PrintWriter out = response.getWriter();
            int i = jobDao.delJob(id);
            if (i > 0) {
                out.print(1);
            } else {
                out.print(0);
            }
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

    }

    private void delJobs(HttpServletRequest request, HttpServletResponse response) {
        String ids = request.getParameter("ids");
        var id = ids.split(",");

        HttpSession session = request.getSession();
        try {
            PrintWriter out = response.getWriter();
            for (var ix : id) {
                int i = jobDao.delJob(Integer.parseInt(ix));
                if (i < 1) {
                    out.print(0);
                    return;
                }
            }
            out.print(1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void updJob(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        int id = Integer.parseInt(request.getParameter("id"));

        Job job = new Job(id, name, remark);
        int i = jobDao.updJob(job);

        try {
            PrintWriter out = response.getWriter();
            if (i > 0) {
                out.print(1);
            } else {
                out.print(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void addJob(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        Job job = new Job(name, remark);
        int i = jobDao.addJob(job);

        try {
            PrintWriter out = response.getWriter();
            if (i > 0) {
                out.print(1);
            } else {
                out.print(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void cheackJobName(HttpServletRequest request, HttpServletResponse response) {
        // 取出用户输入的条件
        String name = request.getParameter("name");
        boolean flag =jobDao.checkJobName(name);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            if (flag) {
                out.print(0);
            } else {
                out.print(1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void jobList(HttpServletRequest request, HttpServletResponse response) {
        //处理乱码问题
        response.setCharacterEncoding("UTF-8");
        //通过响应的response获取打印输出流
        PrintWriter out = null;
        try {
            out = response.getWriter();
            // 取出用户输入的条件
            String name = request.getParameter("name");
            //调用查询方法
            int page = Integer.parseInt(request.getParameter("page"));
            int limit = Integer.parseInt(request.getParameter("limit"));
            List<Job> jobs = jobDao.jobList(name, page, limit);
            int count = jobDao.count();
            //lauyi框架返回四个参数
            R r = new R();
            r.put("msg", "查询成功");
            r.put("data", jobs);
            r.put("count", count);
            r.put("code", 0);
            //将对象返回到页面上，响应数据--respons--打印输出流
            out.print(new Gson().toJson(r));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("dopost");
        doGet(request, response);
    }
}
